// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/

// See page 8.
//!+

// Dup1 prints the text of each line that appears more than
// once in the standard input, preceded by its count.
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"strconv"
)

const (
	STRHELP = "help"
)

type Env struct {
	loglv int
	recmsg bool
	sendmsg bool
}

var UserEnv Env

func cmdHelp () {
	fmt.Println("nstar_cmd help:")
	fmt.Println("L : set loglv")
	fmt.Println("R : set recmsg dbg on/off")
	fmt.Println("S : set sendmsg dbg on/off")
}

func cmdSet (cmd []string) {
	var s1, s2 string
	s1 = cmd[1]
	s2 = cmd[2]
	switch s1 {
	case "L":
		i, err := strconv.Atoi(s2)
		if err == nil {
			UserEnv.loglv = i	
		}
	case "R":
		if s2 == "on" || s2 == "ON" {
			UserEnv.recmsg = true
		}else if s2 == "off" || s2 == "OFF" {
			UserEnv.sendmsg = false
		}
	case "S":
		if s2 == "on" || s2 == "ON" {
			UserEnv.sendmsg = true
		}else if s2 == "off" || s2 == "OFF" {
			UserEnv.sendmsg = false
		}
	}
	fmt.Println(UserEnv)
}

func echoHead () {
	fmt.Print("nstar_cmd:")
}


func echo() {
	fmt.Println("init ok")
	input := bufio.NewScanner(os.Stdin)
	echoHead()
	for input.Scan() {
		
		cmdstr := input.Text()
		cmd := strings.Split(cmdstr, " ")
		switch cmd[0] {
			case "set":
				if len(cmd) < 3 {
					fmt.Println("cmd argc < 3")
					cmdHelp()
				}else {
					cmdSet(cmd)
				}
				
			case "help":
			default: 
				cmdHelp()	
		}
		echoHead()
	}
}


func main() {
	for {
		echo()
	}

}

//!-
